package com.hsytech.baoinbao.unwaste;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.hsytech.baoinbao.DropDownListView;
import com.hsytech.baoinbao.HomeActivity;
import com.hsytech.baoinbao.R;
import com.hsytech.baoinbao.SharedPreferencesTools;
import com.hsytech.baoinbao.base.BaseActivity;
import com.hsytech.baoinbao.volleytool.ApiUrls;
import com.hsytech.baoinbao.volleytool.HttpUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 123 on 2016/3/11.
 */
public class WasteUnStorageActivity extends BaseActivity {
    private LinearLayout home_next;
    private List<Map<String, String>> persons = new ArrayList<>();
    private DropDownListView dropDownListView;
    private String area_id, area_name;
    private HttpUtil.HttpCallback httpCallback = new HttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, JSONObject response) {
            JSONArray result = response.optJSONArray("result");
            for (int i = 0; i < result.length(); i++) {

                String name = result.optJSONObject(i).optString("name");
                String id = result.optJSONObject(i).optString("id");
                Map<String, String> hashMap = new HashMap<>();
                hashMap.put("name", name);
                hashMap.put("id", id);
                persons.add(hashMap);
            }

            String defaultString = null;
            if (persons == null || persons.isEmpty() || persons.size() == 0) {
                defaultString = "";
            } else {
                area_id = result.optJSONObject(0).optString("id");
                defaultString = persons.get(0).get("name");
            }
            //我对这个函数做了改动，让它能够显示默认项“请选择你的学校”
            dropDownListView.setItemsData(defaultString, persons);
//            spinnerAdapter.notifyDataSetChanged();
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            showToast(R.string.msg_http_failure);
        }

        @Override
        public void onErrorMsg(int what, String msg) {
            showToast(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_waste_un_storage);
        setCenterTitleText("商品出库");
        setLeft_button(true);
        dropDownListView = (DropDownListView) findViewById(R.id.drop_down_list_view);
        initSpinner();
        home_next = F(R.id.home_next);
        home_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (persons == null || persons.isEmpty() || persons.size() == 0) {
                    showToast("暂无区域可选");
                } else {
                    Log.d("mylog", "获取到的id" + dropDownListView.getAreaId());
                    if (dropDownListView.getAreaId() == null || dropDownListView.getAreaId().equals("")) {

                    } else {
                        area_id = dropDownListView.getAreaId();
                        area_name = dropDownListView.getAreaText();
                    }
                    Intent intent = new Intent(WasteUnStorageActivity.this, WasteUnStorageListActivity.class);
                    intent.putExtra("area_id", area_id);
                    intent.putExtra("area_name", area_name);
                    startActivity(intent);
                }

            }
        });
    }

    private void initSpinner() {
        Map<String, String> data = new HashMap<>();
        data.put("token", SharedPreferencesTools.getTOKEN(WasteUnStorageActivity.this));
        HttpUtil.httpGetWithParams(0, ApiUrls.GET_AREA_LIST + "?" + "token=" + SharedPreferencesTools.getTOKEN(WasteUnStorageActivity.this), data, httpCallback, WasteUnStorageActivity.this);
    }


}
